/**
 * Created by yuweimei on 2018/4/29.
 */
app.controller('review_more', function($scope, $http) {
	$(".review_bottom input").keyup(function() {
		$(".send").css("background", "#31c27c");
	})
	$(".review_bottom input").blur(function() {
		$(".send").css("background", " rgba(0,0,0,0.4)");
	})
	if($(".review_bottom input").val() == "") {
		$(".send").css("background", " rgba(0,0,0,0.4)");
	}
	var token = localStorage.getItem('token');
	var commentId = sessionStorage.getItem("commentInfoId");
	$scope.commentReplyList = [];
	$scope.commentInfo = function(page) {
		$scope.page = page ? page : 1;
		// 若该加载为初始化加载，即没有传入page参数
		if(!page) {
			$scope.page = 1;
		}
		$http({
			method: 'get',
			url: app.netPath + '/dynamic/reply',
			headers: {
				'Authorization': token
			},
			params: {
				page: $scope.page,
				commentId: commentId
			}
		}).then(function(res) {

			console.log(res.data);
			if(!res.data.error) {
				var infiniteScroll = $('.infinite-scroll');
				var infiniteScrollPreloader = $('.infinite-scroll-preloader');
				$scope.replieduserNickName = res.data.data[0].user1.userNickName;
				$scope.repliedUserId = res.data.data[0].user1.userId;
				$scope.parentId = res.data.data[0].comment.parentId;
				$scope.tourdynamicsId = res.data.data[0].comment.tourdynamicsId;
				for(var i = 0; i < res.data.data.length; i++) {
					if(res.data.data[i].user1.userAvatarAddress) {
						res.data.data[i].user1.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].user1.userAvatarAddress;
					}
					if(res.data.data[i].user2.userAvatarAddress) {
						res.data.data[i].user2.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].user2.userAvatarAddress;
					}
					if(res.data.data[i].user.userAvatarAddress) {
						res.data.data[i].user.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].user.userAvatarAddress;
					}
					$scope.commentReplyList.push(res.data.data[i]);
				}

				console.log($scope.commentReplyList);
				// 启用无限加载事件
				$.attachInfiniteScroll(infiniteScroll);
				$scope.thispage = $scope.commentReplyList.length % 10 == 0 ? $scope.commentReplyList.length / 10 : $scope.commentReplyList.length / 10 + 1;
				$scope.thispage = parseInt($scope.thispage);
				console.log($scope.thispage);

				if(res.data.allPage <= $scope.thispage) {
					// 加载完毕，则注销无限加载事件，以防不必要的加载
					$.detachInfiniteScroll(infiniteScroll);
					// 删除加载提示符
					infiniteScrollPreloader.hide();

				}
				$scope.isOnloading = false;
			}

		})

	};
	//添加回复
	$scope.replyComment = "";
	$scope.replieduserNickName = "";
	$scope.repliedUserId = null;
	$scope.parentId = null;
	$scope.tourdynamicsId = null;
	$scope.addReply = function() {
		if(!token) {
			//判断有无证书
			$.toast("亲，登录个彡");
			// location.href = '#login';
			return;
		} else {
			if($scope.replyComment != "") {
				$scope.comment = {
					"tourdynamicsId": $scope.tourdynamicsId,
					'replayUserid': $scope.repliedUserId,
					'parentId': $scope.parentId,
					'commentContent': $scope.replyComment
				};
				console.log($scope.comment);
				$http({
					method: 'POST',
					url: app.netPath + '/comment/add',
					headers: {
						Authorization: token
					},
					data: $scope.comment
				}).then(function(res) {
					if(!res.data.error) {
						$.toast('评论成功！');
						$scope.replyComment = "";
						$scope.commentReplyList = [];
						$scope.commentInfo();
					} else {
						$.toast(res.data.msg)
					}
				})
			} else {
				$.toast("评论不能为空哟");
			}
		}
	};
	//选中被回复对象
	$scope.updateReplyperson = function(repliedUserId, parentId, userNickName) {
		$scope.replieduserNickName = userNickName;
		$scope.repliedUserId = repliedUserId;
		$scope.parentId = parentId;
	};

	//跳转otherPerson详情页
	$scope.GoToOtherUser = function(userId) {
		sessionStorage.setItem('OtherUserId', userId);
	};
});